<?php
require_once(dirname(__FILE__) . "../../configs/configs.php");
require_once(dirname(__FILE__) . "/functions.inc.php");
/*****************************************************************/
function fetchDistinctCities()
{
	global $conn;	
	$query_recordset = "SELECT tbl_seller.city,tbl_seller.id,tbl_seller.name as retailerName,tbl_product.name,tbl_product.brandName 
						FROM tbl_seller 
						INNER JOIN tbl_product
						ON tbl_seller.id=tbl_product.id";
	$recordset = mysql_query($query_recordset, $conn);	
	
	if ($recordset)
	{
		$toret = array();
		while ($row_recordset = mysql_fetch_assoc($recordset)) 
		{
			array_push($toret, $row_recordset);
		}
	}
	else
	{
		$toret = array(
			"data" => array("error" => mysql_error()), 
			"metadata" => array()
		);
	}
	return json_encode(array('rows'=>$toret));
}
/*****************************************************************/
function fetchHomeBrandTilesInfo()
{
	global $conn;
	//$query_recordset ="SELECT * FROM tbl_seller GROUP BY name";
	$query_recordset ="select * from tbl_product group by brandName";	
	$recordset = mysql_query($query_recordset, $conn);		
	if ($recordset)
	{
		$toret = array();
		while ($row_recordset = mysql_fetch_assoc($recordset)) 
		{
			array_push($toret, $row_recordset);
		}
	}
	else
	{
		$toret = array(
			"data" => array("error" => mysql_error()), 
			"metadata" => array()
		);
	}
	return json_encode(array('rows'=>$toret));
}
//************************************************************************************/	
function fetchHomeRetailerTilesInfo()
{
	global $conn;
	//$query_recordset ="SELECT * FROM tbl_seller GROUP BY name";
	$query_recordset ="select * from tbl_seller group by name;";
	
	$recordset = mysql_query($query_recordset, $conn);	
	
	if ($recordset)
	{
		$toret = array();
		while ($row_recordset = mysql_fetch_assoc($recordset)) 
		{
			array_push($toret, $row_recordset);
		}
	}
	else
	{
		$toret = array(
			"data" => array("error" => mysql_error()), 
			"metadata" => array()
		);
	}
	return json_encode(array('rows'=>$toret));
}
//************************************************************************************/	
function fetchProductInfoByCity()
{
	global $conn;
	$cityName = @$_REQUEST["cityName"];	
	$srchItem = strtoupper(@$_REQUEST["srchItem"]);		
	if($cityName === 'All' or $cityName === "")
	{							
		$query ="SELECT 
						t.name AS prodName,
						t.id,
						t.category,
						t.subcategory,
						t.desc AS prodDesc,
						t.brandName,
						t.price AS originalPrice,
						t.thumbnail_path,
						t.city,
						t.offerPrice
					from
						(SELECT 
								t1.name,
								t1.id,
								t1.category,
								t1.subcategory,
								t1.desc,
								t1.brandName,
								t1.price,
								t1.thumbnail_path,
								t2.name as retailerName,
								t2.city,
								t3.offerPrice
						from
							tbl_product t1, tbl_seller t2, tbl_offer t3, tbl_seller_product t4, tbl_product_offer t5
						where
							t1.id = t4.product_id
								and t2.id = t4.seller_id
								and t1.id = t5.product_id
								and t3.id = t5.offer_id) t
					where								
							(t.name = '$srchItem'			
							or t.category = '$srchItem'
							or t.subcategory = '$srchItem'
							or t.retailerName = '$srchItem'
							or t.brandName = '$srchItem')";
	}
	else
	{				
		$query ="SELECT 
				t.name AS prodName,
				t.id,
				t.category,
				t.subcategory,
				t.desc AS prodDesc,
				t.brandName,
				t.price AS originalPrice,
				t.thumbnail_path,
				t.city,
				t.offerPrice
			from
				(SELECT 
					t1.name,
						t1.id,
						t1.category,
						t1.subcategory,
						t1.desc,
						t1.brandName,
						t1.price,
						t1.thumbnail_path,
						t2.name as retailerName,
						t2.city,
						t3.offerPrice
				from
					tbl_product t1, tbl_seller t2, tbl_offer t3, tbl_seller_product t4, tbl_product_offer t5
				where
					t1.id = t4.product_id
						and t2.id = t4.seller_id
						and t1.id = t5.product_id
						and t3.id = t5.offer_id) t
			where	
					t.city ='$cityName'
					and (t.name = '$srchItem'			
					or t.category = '$srchItem'
					or t.subcategory = '$srchItem'
					or t.retailerName = '$srchItem'
					or t.brandName = '$srchItem')";
	}
	
		
    $query_recordset = sprintf($query);
	//echo $query_recordset;
	$recordset = mysql_query($query_recordset, $conn);	
	
	if ($recordset)
	{
		$toret = array();
		while ($row_recordset = mysql_fetch_assoc($recordset)) 
		{
			array_push($toret, $row_recordset);
		}
	}
	else
	{
		$toret = array(
			"data" => array("error" => mysql_error()), 
			"metadata" => array()
		);
	}
	return json_encode(array('rows'=>$toret));
}
//************************************************************************************/
function fetchDetailedProductInfo()
{
	global $conn;
	$prodId = @$_REQUEST["prodId"];
		
	$query ="SELECT tbl_seller.name AS sellerName,tbl_seller.info,tbl_seller.city,tbl_seller.addr,tbl_seller.contactNo, tbl_product.name AS prodName,tbl_product.image_path,tbl_product.thumbnail_path,tbl_product.desc,tbl_product.brandName,tbl_product.price,tbl_offer.name AS offerName,tbl_offer.type,tbl_offer.desc AS offerDesc,tbl_offer.offerPrice
			FROM tbl_seller
			LEFT JOIN tbl_product 
			ON tbl_seller.id=tbl_product.id
			LEFT JOIN tbl_offer
			ON tbl_product.id = tbl_offer.id
			WHERE tbl_product.id ='$prodId'";	
	
    $query_recordset = sprintf($query);	
	$recordset = mysql_query($query_recordset, $conn);	
	
	if ($recordset)
	{
		$toret = array();
		while ($row_recordset = mysql_fetch_assoc($recordset)) 
		{
			array_push($toret, $row_recordset);
		}
	}
	else
	{
		$toret = array(
			"data" => array("error" => mysql_error()), 
			"metadata" => array()
		);
	}
	return json_encode(array('rows'=>$toret));
}
//************************************************************************************/
function fetchProductDetailsBySearchKeyWord(){
	global $conn;	
	$searchKeyWord = @$_REQUEST["searchKeyWord"];	
	$query_recordset = "select distinct brandName from tbl_product WHERE category ='$searchKeyWord' OR subcategory = '$searchKeyWord'";	
	$recordset = mysql_query($query_recordset, $conn);		
	if ($recordset)
	{
		$toret = array();
		while ($row_recordset = mysql_fetch_assoc($recordset)) 
		{
			array_push($toret, $row_recordset);
		}
	}
	else
	{
		$toret = array(
			"data" => array("error" => mysql_error()), 
			"metadata" => array()
		);
	}
	return json_encode(array('rows'=>$toret));
}
//************************************************************************************/
function filterSearchedProducts()
{
	global $conn;
	$brandName = @$_REQUEST["brandName"];	
	$srchItem = strtoupper(@$_REQUEST["srchItem"]);		
	$cityName = @$_REQUEST["cityName"];	
		
	//echo $brandName.'----'.$srchItem.'----'.$cityName.'<br/>';
	if($cityName == '' or $cityName == 'All'){		
		$query ="SELECT 
						t.name AS prodName,
						t.id,
						t.category,
						t.subcategory,
						t.desc AS prodDesc,
						t.brandName,
						t.price AS originalPrice,
						t.thumbnail_path,
						t.city,
						t.offerPrice
					from
						(SELECT 
							t1.name,
								t1.id,
								t1.category,
								t1.subcategory,
								t1.desc,
								t1.brandName,
								t1.price,
								t1.thumbnail_path,
								t2.city,
								t3.offerPrice
						from
							tbl_product t1, tbl_seller t2, tbl_offer t3, tbl_seller_product t4, tbl_product_offer t5
						where
							t1.id = t4.product_id
								and t2.id = t4.seller_id
								and t1.id = t5.product_id
								and t3.id = t5.offer_id) t
					where									
								t.brandName in ('$brandName')
								and (t.name = '$srchItem'			
								or t.category = '$srchItem'
								or t.subcategory = '$srchItem'								
								)";
	}
	else{	
		$query ="SELECT 
						t.name AS prodName,
						t.id,
						t.category,
						t.subcategory,
						t.desc AS prodDesc,
						t.brandName,
						t.price AS originalPrice,
						t.thumbnail_path,
						t.city,
						t.offerPrice
					from
						(SELECT 
							t1.name,
								t1.id,
								t1.category,
								t1.subcategory,
								t1.desc,
								t1.brandName,
								t1.price,
								t1.thumbnail_path,
								t2.city,
								t3.offerPrice
						from
							tbl_product t1, tbl_seller t2, tbl_offer t3, tbl_seller_product t4, tbl_product_offer t5
						where
							t1.id = t4.product_id
								and t2.id = t4.seller_id
								and t1.id = t5.product_id
								and t3.id = t5.offer_id) t
					where	
								t.city ='$cityName'
								and t.brandName in ('$brandName')
								and (t.name = '$srchItem'			
								or t.category = '$srchItem'
								or t.subcategory = '$srchItem'								
								)";
	}
	
							
    $query_recordset = sprintf($query);
	//echo $query_recordset;
	$recordset = mysql_query($query_recordset, $conn);	
	
	if ($recordset)
	{
		$toret = array();
		while ($row_recordset = mysql_fetch_assoc($recordset)) 
		{
			array_push($toret, $row_recordset);
		}
	}
	else
	{
		$toret = array(
			"data" => array("error" => mysql_error()), 
			"metadata" => array()
		);
	}
	return json_encode(array('rows'=>$toret));
}
//************************************************************************************/
$ret = array(
	"data" => array("error" => "No operation"), 
	"metadata" => array()
);
if ($conn === false)
{
	$ret = array(
		"data" => array("error" => "database connection error, please check your settings !"), 
		"metadata" => array()
	);
} 
else
{
	switch (@$_REQUEST["method"])
	{		
		case "fetchDistinctCities":
			$ret = fetchDistinctCities();
		break;
		case "fetchProductInfoByCity":
			$ret =fetchProductInfoByCity();
		break;
		case "fetchDetailedProductInfo":
			$ret =fetchDetailedProductInfo();
		break;
		case "fetchHomeBrandTilesInfo":
			$ret =fetchHomeBrandTilesInfo();
		break;
		case "fetchHomeRetailerTilesInfo":
			$ret =fetchHomeRetailerTilesInfo();
		break;
		case "fetchProductDetailsBySearchKeyWord":
			$ret =fetchProductDetailsBySearchKeyWord();
		break;	
		case "filterSearchedProducts":
			$ret =filterSearchedProducts();
		break;	
	}
}

/*******************************************************************************************************************************/
echo $ret;
die();

?>
